//
//  FDAddCaseController.m
//  FamousDoctorOnline
//
//  Created by 丁鼎 on 16/7/7.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDAddCaseController.h"
#import "FDAddManageView.h"
#import "FDDiseaseKindsController.h"
#import "FDCaseModel.h"
#import "SVProgressHUD.h"
#import <MJExtension.h>

@interface FDAddCaseController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,weak) FDAddManageView *addView;
@property (nonatomic,strong)NSArray *diseaseArray;

@property (nonatomic,strong)NSArray *baseArray;

@end

@implementation FDAddCaseController

-(FDAddManageView *)addView
{
    if(!_addView)
    {
        FDAddManageView *addView= [[FDAddManageView alloc]init];
        _addView = addView;
        
        [self.view addSubview:addView];
        
        __weak typeof(self)weakSelf = self;
        
        self.addView.tempBlock = ^(BOOL isBase){
            
            FDDiseaseKindsController *vc = [[FDDiseaseKindsController alloc]init];
            
            vc.kindArray = isBase ? self.baseArray : self.diseaseArray;
            
            vc.returnBlock = ^(NSString * returnString){
                
                if(vc.kindArray.count >  5)
                {
                    weakSelf.addView.baseNameLabel.text = returnString;
                }
                else{
                    weakSelf.addView.diseaseNameLabel.text = returnString;
                }
            };
            
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        
        self.addView.photoBlock = ^{
          
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            picker.delegate = self;
            
            [self presentViewController:picker animated:YES completion:nil];
            
        };
        
    }
    return _addView;
}

-(NSArray *)diseaseArray{
    if(_diseaseArray == nil)
    {
        _diseaseArray= @[@"肿瘤科",@"心血管科",@"血液科",@"神经科",@"骨科"];
    }
    return _diseaseArray;
    
}

-(NSArray *)baseArray
{
    if(!_baseArray)
    {
        _baseArray = @[@"第一行",@"第二行",@"第三行",@"第四行",@"第五行",@"第六行",@"第七行",@"第八行",@"第九行",@"第十行",@"第十一行",@"第十二行",@"第十三行",@"第十四行",@"第十五行",@"第十六行",@"第十七行"];
    }
    return _baseArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor  = FDColor(245, 245, 248);
    
    self.title = @"增加病例";
    
    [self setupUI];
    
    UIBarButtonItem *commit = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(clickCommit)];
    commit.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem = commit;
}

-(void)setupUI
{
    
    [self.addView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.right.bottom.mas_equalTo(0);
    }];
    
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [self.addView.photo setBackgroundImage:info[@"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal];
    
    [picker dismissViewControllerAnimated:YES completion:^{
        [self.addView.photo setImage:info[@"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal];
    }];
}

-(void)clickCommit
{
    if([self.addView.diseaseNameLabel.text isEqualToString:@"请选择 :"] || [self.addView.baseNameLabel.text isEqualToString:@"请选择 :"])
    {
        [SVProgressHUD showInfoWithStatus:@"请填写疾病细分"];
        return;
    }
    
    FDCaseModel *model = [[FDCaseModel alloc]init];
    model.diseaseKind = self.addView.diseaseNameLabel.text;
    model.baseKind = self.addView.baseNameLabel.text;
    model.detail = self.addView.distributeView.text;
    if(self.addView.photo.currentImage)
    {
        model.photo = self.addView.photo.currentImage;
    }
    
    NSDate *date =[NSDate date];
    NSDateFormatter *formatter = [NSDateFormatter new];
    formatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    model.currentTime = [formatter stringFromDate:date];
    NSMutableDictionary *dict =[model mj_keyValues];
    
    NSString *filePath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
    
    
    
    [dict writeToFile:filePath atomically:NO];
    
    if(_modelBlock)
    {
        _modelBlock(model);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
